import java.util.Date;

import net.sf.json.JSONObject;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.autolinq.util.http.HttpClientConnectionManager;
  
public class Test {  
  
    public static String APPID,APPSECRET;  
      
    //http客户端    
    public static DefaultHttpClient httpclient;  
      
    static {    
        httpclient = new DefaultHttpClient();    
        httpclient = (DefaultHttpClient) HttpClientConnectionManager.getSSLInstance(httpclient); // 接受任何证书的浏览器客户端  
        APPID = "";//你的APPID  
        APPSECRET = ""; //你的APPSECRET  
    }    
      
    /** 
     * 创建菜单  
     */  
    public static String createMenu(String params, String accessToken) throws Exception {    
        HttpPost httpost = HttpClientConnectionManager.getPostMethod("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken);    
        httpost.setEntity(new StringEntity(params, "UTF-8"));    
        HttpResponse response = httpclient.execute(httpost);    
        String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");    
        JSONObject demoJson = JSONObject.fromObject(jsonStr);
        return demoJson.getString("errmsg");  
    }    
    /** 
     * 设备授权
     */  
    public static String authDevice(String params, String accessToken) throws Exception {    
        HttpPost httpost = HttpClientConnectionManager.getPostMethod("https://api.weixin.qq.com/device/authorize_device?access_token=" + accessToken);    
        httpost.setEntity(new StringEntity(params, "UTF-8"));    
        HttpResponse response = httpclient.execute(httpost);    
        String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");    
        JSONObject demoJson = JSONObject.fromObject(jsonStr);
        return demoJson.toString();  
    }  
    
    /** 
     * 获取二维码
     */  
    public static String createQRCode(String params, String accessToken) throws Exception {    
        HttpPost httpost = HttpClientConnectionManager.getPostMethod("https://api.weixin.qq.com/device/create_qrcode?access_token=" + accessToken);    
        httpost.setEntity(new StringEntity(params, "UTF-8"));    
        HttpResponse response = httpclient.execute(httpost);    
        String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");    
        JSONObject demoJson = JSONObject.fromObject(jsonStr);
        return demoJson.toString();  
    }
    /** 
     * 获取accessToken  
     */  
    public static String getAccessToken(String appid, String secret) throws Exception {    
        HttpGet get = HttpClientConnectionManager.getGetMethod("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret);    
        HttpResponse response = httpclient.execute(get);    
        String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");  
        JSONObject demoJson = JSONObject.fromObject(jsonStr);
        return demoJson.getString("access_token");  
    }    
    /** 
     * 查询菜单 
     */  
    public static String getMenuInfo(String accessToken) throws Exception {    
        HttpGet get = HttpClientConnectionManager.getGetMethod("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + accessToken);    
        HttpResponse response = httpclient.execute(get);    
        String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");    
        return jsonStr;    
    }   
    
    /** 
     * 查询菜单 
     */  
    public static String getOpenIds(String accessToken) throws Exception {    
        HttpGet get = HttpClientConnectionManager.getGetMethod("https://api.weixin.qq.com/cgi-bin/user/get?next_openid=&access_token=" + accessToken);    
        HttpResponse response = httpclient.execute(get);    
        String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");    
        return jsonStr;    
    }  
      
    /**  
     * 删除菜单  
     */    
    public static String deleteMenuInfo(String accessToken) throws Exception {    
        HttpGet get = HttpClientConnectionManager.getGetMethod("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + accessToken);    
        HttpResponse response = httpclient.execute(get);    
        String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");    
        JSONObject demoJson = JSONObject.fromObject(jsonStr);  
        return demoJson.getString("errmsg");    
    }    
      
   /* 
    * 测试 
    */  
    public static void main(String[] args) {  
  
        StringBuffer sb = new StringBuffer();  
        sb.append("{");  
        sb.append(" \"button\":[");  
        sb.append("     {");  
        sb.append("         \"name\":\"旅程\",");       //第一个菜单  
        sb.append("         \"sub_button\":[");  
        sb.append("             {");  
        sb.append("                 \"type\":\"click\",");  
        sb.append("                 \"name\":\"创建旅程\",");  
        sb.append("                 \"key\":\"music\"");  
        sb.append("             },");  
        sb.append("             {");  
        sb.append("                 \"type\":\"click\",");  
        sb.append("                 \"name\":\"旅程列表\",");  
        sb.append("                 \"key\":\"tripList\"");  
        sb.append("             }");    
        sb.append("         ]");  
        sb.append("     },");  
        sb.append("     {");  
        sb.append("         \"name\":\"搭便车\",");  
        sb.append("         \"sub_button\":[");  
        sb.append("             {");  
        sb.append("                 \"type\":\"click\",");  
        sb.append("                 \"name\":\"找便车\",");  
        sb.append("                 \"key\":\"createPickup\"");  
        sb.append("             },");  
        sb.append("             {");  
        sb.append("                 \"type\":\"view\",");  
        sb.append("                 \"name\":\"便车库\",");  
        sb.append("                 \"url\":\"");    //连接地址  
        sb.append("             }");  
        sb.append("         ]");  
        sb.append("     },");  
        sb.append("     {");  
        sb.append("         \"name\":\"我的\",");             //URL 连接  
        sb.append("         \"sub_button\":[");  
        sb.append("             {");  
        sb.append("                 \"type\":\"view\",");  
        sb.append("                 \"name\":\"当前位置\",");  
        //sb.append("                 \"url\":\"http://114.141.164.151/DP/TripMap?tripID=145&openID=ogK2Fjpr22CvUXl8Q0eMHC8LcJTA&headImgUrl=http://wx.qlogo.cn/mmopen/FpflLl4VVYhKneKpuI6cgytEhm2FAHGrhvLUBu7at67wJtYyZXPEE4XBcWus2TDGhkps6CQXSjcuwibhtkuQeEcBTXnHZV5ak/0&city=\",");    //连接地址  
        sb.append("                 \"url\":\"http://203.195.191.112/device/\",");    //连接地址  
        sb.append("             }");  
        sb.append("         ]");  
        sb.append("     }");  
        sb.append(" ]");  
        sb.append("}");  
 

          
        try {  
            // 获取accessToken -参数appid，secret    
            String accessToken = getAccessToken(APPID, APPSECRET);    
            String res="";  
            res = deleteMenuInfo(accessToken);
            res = createMenu(sb.toString(), accessToken);   
            res = getMenuInfo(accessToken);
            System.out.println(res);    
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
  
    }  
  
}  